﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Najak;

namespace Falon
{
	public abstract partial class UnitCommand
	{
		public class Attack : UnitCommand
		{
			public Entity TargetEntity;
			public Vector2 TargetLocation;
			public float DistanceRemaining;
			private float _timeRemaining;
			public Vector2 Direction;
			public float TargetBuffer;
			public bool IsCollidingWithTarget;

			public Attack(Entity targetEntity)
			{
				TargetEntity = targetEntity;
			}

			public override float TimeRemaining
			{
				get { return _timeRemaining; }
			}
			protected override void _OnStarted()
			{
				
				base._OnStarted();
				TargetBuffer = TargetEntity.BoundingRadius + Owner.BoundingRadius + Owner.Weapon.Range;
				_CalculateMovement();
			}
			protected override float OnUpdate(float time)
			{
				if (TargetEntity.Health <= 0)
				{
					_timeRemaining = 0;
					return time;
				}
				Owner.Direction = Direction;

				_CalculateMovement();
				if (time >= TimeRemaining)
				{
					_CalculateMovement();
					if (time >= TimeRemaining)
					{
						if (Game.Instance.Seconds >= Owner.EarliestTimeForNextAttack)
						{
							if (Owner.Weapon.Name == "Spear")
							{
								Animation anim = new Animation(Owner.Weapon, PokeAnimation);
								this.Activities.Add(anim);
							}
							else
							{
								Animation anim = new Animation(Owner.Weapon, PokeAnimation);
								this.Activities.Add(anim);
							}
							if (Owner.Weapon.Projectile != null)
							{
								Projectile p = Owner.Weapon.Projectile.Clone();
								p.AddToScene(Owner.Scene, Owner.Weapon, Owner.Location, TargetEntity);
							}
							else
							{
								int damage = (int)Owner.Weapon.Damage - (int)Owner.Armor.Defense;
								if (damage >= 0)
									TargetEntity.TakeDamage(damage, Owner);
							}

							Owner.EarliestTimeForNextAttack = Game.Instance.Seconds + Owner.Weapon.AttackRate;

						
						}
						//Owner.Location = TargetLocation;
						float timeunused = time - TimeRemaining;
						_timeRemaining = 1;
						
						return timeunused;
					}
				}
				float adjustedSpeed = (Owner.MovementSpeed - Owner.Armor.Weight);
				if (adjustedSpeed >= 0)
				{
					Vector2 offset = Direction * time * adjustedSpeed;
					Owner.Location += offset;
					_timeRemaining -= time;
					return 0;
				}
				else
					return time;
			}

			public override void Attach(Unit owner)
			{
				base.Attach(owner);
				_CalculateMovement();
			}

			private void _CalculateMovement()
			{
				TargetLocation = TargetEntity.Location;

				float dx = TargetLocation.X - Owner.Location.X;
				float dy = TargetLocation.Y - Owner.Location.Y;
				DistanceRemaining = (float)Math.Sqrt((dx * dx) + (dy * dy));
				Direction = new Vector2(dx / DistanceRemaining, dy / DistanceRemaining);
				DistanceRemaining -= TargetBuffer;
				float adjustedSpeed = (Owner.MovementSpeed - Owner.Armor.Weight);
				_timeRemaining = (adjustedSpeed < 0) ? 0 : DistanceRemaining / adjustedSpeed;
			}

			static public List<Frame> PokeAnimation = new List<Frame>(new Frame[]
				{
					new Frame(0, new Vector2(), 0),
					new Frame(0, new Vector2(0, 30), 0.15f),
					new Frame(0, new Vector2(), 0.15f),
					new Frame(0, new Vector2(), 0.1f)
				}
			);
			
			static public List<Frame> BatAnimation = new List<Frame>(new Frame[]
				{
					new Frame(1.5f, new Vector2(), 0),
					new Frame(-0.2f, new Vector2(0, 30), 0.15f),
					new Frame(1.5f, new Vector2(), 0.15f),
					new Frame(0, new Vector2(), 0.1f)
				}
			);


		}
	}
}
